package question3;

import java.util.ArrayList;
import java.util.List;

public class Student {
    private String name;
    private School school;
    private List<Subject> subjects = new ArrayList<>();

    // 构造时关联学院
    public Student(String name, School school) {
        this.name = name;
        this.school = school;
        school.addStudent(this);    // 反向关联，学院添加自身
    }

    public void setSchool(School school) {
        this.school = school;
        school.addStudent(this);
    }

    // 添加选修课程（限制1-5门）
    public void addSubject(Subject subject) {
        if (subjects.size() < 5) {
            subjects.add(subject);
            subject.addStudent(this);  // 反向关联课程
        } else {
            System.out.println("学生最多选修5门课程");
        }
    }
}
